package com.sas.web;

import java.util.Locale;

import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.PropertyModel;

import com.sas.web.panel.MenuPanel;

public class SasBasePage extends BasePage {
	@SuppressWarnings("serial")
	public SasBasePage () {
		add(new Link<Void>("sas") {
			@Override
			public void onClick() {
				setResponsePage(SasBasePage.class);
			}
		});
		add(new MenuPanel("menuPanel"));
		 // Dropdown for selecting locale
        add(new LocaleDropDownChoice("localeSelect"));
	}
	
	 /**
     * Sets locale for the user's session (getLocale() is inherited from Component)
     * 
     * @param locale
     *            The new locale
     */
    public void setLocale(Locale locale)
    {
        if (locale != null)
        {
            getSession().setLocale(locale);
        }
    }
	
	/**
     * Dropdown with Locales.
     */
    private final class LocaleDropDownChoice extends DropDownChoice<Locale> {
        /**
		 * 
		 */
		private static final long serialVersionUID = -4347050349898958877L;

		/**
         * Construct.
         * 
         * @param id
         *            component id
         */
        public LocaleDropDownChoice(String id)
        {
            super(id, SasApplication.LOCALES, new LocaleChoiceRenderer());

            // set the model that gets the current locale, and that is used for
            // updating the current locale to property 'locale' of FormInput
            setDefaultModel(new PropertyModel<Locale>(SasBasePage.this, "locale"));
        }

        /**
         * @see org.apache.wicket.markup.html.form.DropDownChoice#onSelectionChanged(java.lang.Object)
         */
        @Override
        public void onSelectionChanged(Locale newSelection)
        {
            // note that we don't have to do anything here, as our property
            // model allready calls FormInput.setLocale when the model is
            // updated

            // force re-render by setting the page to render to the bookmarkable
            // instance, so that the page will be rendered from scratch,
            // re-evaluating the input patterns etc
//            setResponsePage(SasBasePage.class);
        }

        /**
         * @see org.apache.wicket.markup.html.form.DropDownChoice#wantOnSelectionChangedNotifications()
         */
        @Override
        protected boolean wantOnSelectionChangedNotifications()
        {
            // we want roundtrips when a the user selects another item
            return true;
        }
    }
    
    /**
     * Choice for a locale.
     */
    private final class LocaleChoiceRenderer extends ChoiceRenderer<Locale>
    {
        /**
         * Constructor.
         */
        public LocaleChoiceRenderer()
        {
        }

        /**
         * @see org.apache.wicket.markup.html.form.IChoiceRenderer#getDisplayValue(Object)
         */
        @Override
        public Object getDisplayValue(Locale locale)
        {
            String display = locale.getDisplayName(getLocale());
            return display;
        }
    }

}
